<?php
namespace App\Http\Controllers\Admin\Product;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class CategoryController extends BaseController
{
    protected  $api_url = [
        'index' => ['get', 'product/cate'],
        'show' => ['get', 'product/cate/%d'],
        'store' => ['post', 'product/cate'],
        'update' => ['put', 'product/cate/%d'],
        'delete' => ['delete', 'product/cate/%d'],
        
        'attr_index' => ['get', 'product/attr'],
        'recommend' => ['post', 'product/cate/recommend/%d'],
        'shopindex_recommend' => ['post', 'product/cate/shopindex_recommend/%d'],
        'showstatus' => ['post', 'product/cate/showstatus/%d'],
    ];
    
    protected $api_host;
    
    protected $request;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_backend');
    }
    
    public function indexAction(){
        $this->validate($this->request, [
            'page' => 'integer',
            'name' => 'string',
        ]);
        $search_params = $this->request->all(['name']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
//             'orderBy'=>'id' ,
//             'sortedBy'=>'asc',
            'page' => $page,
            'type'=>'all',
        ];
        $categorys = [];
        $data = [];
        if(Redis::exists("categorys")){
            $cateall = Redis::hgetall("categorys");
            if(is_array($cateall)){
                foreach($cateall as $c){
                    $categorys [] = json_decode($c,true);
                }
                
                $categorys = sortArrByManyField($categorys,'root_id',SORT_ASC,'pid',SORT_ASC,'sort',SORT_ASC,'id',SORT_ASC);
            }
        }else{
            $ret = http_admin($this->api_url['index'])->send($params);
            if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
                $categorys = $ret['data']['data'];
                foreach($categorys as $d){
                    Redis::hset("categorys",$d['id'],json_encode($d));
                }
            }
          
        }

        
        
        $data['search_params'] = $search_params;
        $cates['cate1'] = [];
        $cates['cate2'] = [];
        $cates['cate3'] = [];
        $cates['cate2_json']="";
        $cate2_arr = [];
        foreach($categorys as $cate){
            if($cate['pid'] == 0){
                $cates['cate1'][$cate['id']]  = $cate;
            }else{
                if($cate['first_id'] == $cate['pid']){
                    $cates['cate2'][$cate['pid']][$cate['id']]  = $cate;
                    $cate2_arr[$cate['pid']][] = ['id'=>$cate['id'],'name'=> $cate['name']];
                }else{
                    $cates['cate3'][$cate['pid']][$cate['id']]= $cate;
                }
            }
        }
        
        $attr_url = $this->api_host . 'product/attr';
        $params = [
            'type' => 'all',
        ];
        $attr_ret = http_admin($this->api_url['attr_index'])->send($params); 
        $data['cates'] = [];
        if(isset($attr_ret['code']) && $attr_ret['code']===0 && !empty($attr_ret['data'])){
            $cates['attrs'] = $attr_ret['data']['data'];
        }
        $cates['cate2_json'] = json_encode($cate2_arr);
     
        return view('Admin.product.categories',$cates);
    }
    
    
    
    
    public function showAction($id){
        echo $id;
    }
    
    public function storeAction(){
        $this->validate($this->request, [
            'name' => 'string',
        ]);
        $data = $this->request->all([]);
      
        $ret = http_admin($this->api_url['store'])->send($data); 
       
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $newdata = http_admin($this->api_url['show'],[$ret['data']['id']])->send(); 
            if(isset($newdata['data']['data'])){
                Redis::hset("categorys",$ret['data']['id'],json_encode($newdata['data']['data']));
            }
            
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function updateAction($id){
        $this->validate($this->request, [
            'name' => 'required'
        ]);
        $data = $this->request->all([]);
        
        $ret = http_admin($this->api_url['update'],[$id])->send($data); 
        
        if(isset($ret['code']) && $ret['code']===0 ){
            $newdata = http_admin($this->api_url['show'],[$id])->send();
            if(isset($newdata['data']['data'])){
                Redis::hset("categorys",$id,json_encode($newdata['data']['data']));
            }
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function deleteAction($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send(); 
        if(isset($ret['code']) && $ret['code']===0){
            Redis::hdel("categorys",$id);
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
    /**
     *  修改类目为推荐类目
     * */
    public function ajaxRecommend($id){
        $data = $this->request->all();
        $ret = http_admin($this->api_url['recommend'],[$id])->send($data);
        if(isset($ret['code']) && $ret['code']===0){
            $this->reSetWebIndexRedis('categorys'); // 清空重置分类redis
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
    /**
     *  掩藏或显示分类
     * */
    
    public function ajaxShowStatus($id){
        $data = $this->request->all();
        $ret = http_admin($this->api_url['showstatus'],[$id])->send($data);
        if(isset($ret['code']) && $ret['code']===0){
            $this->reSetWebIndexRedis('categorys'); // 清空重置分类redis
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }

    /**
     *  修改类目为推荐类目
     * */
    public function ajaxShopIndexRecommend($id){
        $data = $this->request->all();
        $ret = http_admin($this->api_url['shopindex_recommend'],[$id])->send($data);
        if(isset($ret['code']) && $ret['code']===0){
            $this->reSetWebIndexRedis('categorys'); // 清空重置分类redis
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }

}